package fr.utbm.gl52.smartgrid.model.device;

import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

import fr.utbm.gl52.smartgrid.util.Constants;
import fr.utbm.gl52.smartgrid.util.properties.Properties;
import fr.utbm.gl52.smartgrid.util.properties.PropertyDouble;
import fr.utbm.gl52.smartgrid.util.properties.PropertyString;

public abstract class Device implements IDevice {

	protected double power;
	protected double current;
	protected double voltage;
	protected double efficiency;

	private UUID ID;

	protected String name = Messages.getString("Device.0"); //$NON-NLS-1$

	public Device() {
		super();
		this.ID = UUID.randomUUID();
	}

	public Device(String name, double power, double current, double voltage,
			double efficiency) {
		super();
		this.name = name;
		this.power = power;
		this.current = current;
		this.voltage = voltage;
		this.efficiency = efficiency;
		this.ID = UUID.randomUUID();
	}

	public Device(double power, double current, double voltage,
			double efficiency) {
		super();
		this.power = power;
		this.current = current;
		this.voltage = voltage;
		this.efficiency = efficiency;
		this.ID = UUID.randomUUID();
	}

	/**
	 * @see Device.IDevice#getCurrent()
	 */
	public double getCurrent() {
		return this.current;
	}

	public UUID getID() {
		return this.ID;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String s) {
		this.name = s;
	}

	public String getType() {
		return getClass().getSimpleName();
	}

	public Map<String, Properties<?>> getProperties() {
		TreeMap<String, Properties<?>> properties = new TreeMap<String, Properties<?>>();
		PropertyString p0 = new PropertyString(
				Messages.getString("Device.1"), this.name, false); //$NON-NLS-1$
		PropertyDouble p1 = new PropertyDouble(
				Messages.getString("Device.2"), this.power, false); //$NON-NLS-1$
		PropertyDouble p2 = new PropertyDouble(
				Messages.getString("Device.3"), this.current, false); //$NON-NLS-1$
		PropertyDouble p3 = new PropertyDouble(
				Messages.getString("Device.4"), this.voltage, false); //$NON-NLS-1$
		PropertyDouble p4 = new PropertyDouble(
				Messages.getString("Device.5"), this.efficiency, false); //$NON-NLS-1$
		properties.put(p0.getName(), p0);
		properties.put(p1.getName(), p1);
		properties.put(p2.getName(), p2);
		properties.put(p3.getName(), p3);
		properties.put(p4.getName(), p4);
		return properties;
	}

	public void setProperties(Map<String, Properties<?>> map) {
		this.name = (String) map.remove(Messages.getString("Device.1")).getValue(); //$NON-NLS-1$
		this.power = (Double) map.remove(Messages.getString("Device.2")).getValue(); //$NON-NLS-1$
		this.current = (Double) map
				.remove(Messages.getString("Device.3")).getValue(); //$NON-NLS-1$
		this.voltage = (Double) map
				.remove(Messages.getString("Device.4")).getValue(); //$NON-NLS-1$
		this.efficiency = (Double) map
				.remove(Messages.getString("Device.5")).getValue(); //$NON-NLS-1$
		if (map.size() != 0) {
			if (Constants.isDebug)
				System.out
						.println(Messages.getString("Device.6") + map.size()); //$NON-NLS-1$
		}

	}
}
